package com.lanou.controller;

import com.google.gson.Gson;
import com.lanou.service.InsertService;
import com.lanou.utils.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ChangeBookServlet",urlPatterns = "/changebook")
public class ChangeBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    private InsertService insertService = new InsertService();
    //修改图书
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        int bid = Integer.parseInt(String.valueOf(session.getAttribute("bid")));
        String bname = request.getParameter("bname");
        String price = request.getParameter("price");
        String author = request.getParameter("author");
        String cate = request.getParameter("cate");
        AjaxResult ar = new AjaxResult();
        PrintWriter out = response.getWriter();
        int i = insertService.changeBook(bid, bname, price, author, cate);
        if (i==1000){
            ar.setCode(i);
            ar.setMsg("添加成功");
        }else if (i==1001){
            ar.setCode(i);
            ar.setMsg("书名已存在");
        }else if (i==1002){
            ar.setCode(i);
            ar.setMsg("添加失败");
        }
        out.write(new Gson().toJson(ar));
        out.close();
    }
}
